package request;

import core.GameCharacter;
import java.io.OutputStream;

import response.ResponseBuddyRemove;
import core.GameUser;

/**
 * Class that handles request from a user to remove another user
 * from list of his buddies.
 *
 * @author Zoran, brian
 *
 */
public class RequestBuddyRemove extends GameRequest{
	// User who requested to remove the buddy.
	private GameUser sender;
	private GameCharacter toRemove = null;
        private String toRemoveName; // User's character name.

        private int removerId;
        private int toRemoveId;

	public void parse() {
            try {
                // Protocol: (String) CharacterName
                removerId = client.getUser().getCharacter().getID();
                toRemoveName = client.readString(dataIn);
                toRemove = gameServer.getGameDB().getCharacter(toRemoveName);
                if(toRemove != null) {
                    toRemoveId = toRemove.getID();
                    gameServer.getGameDB().removeBuddy(removerId, toRemoveId);
                }

            } catch(Exception e){
                gameServer.log("RequestBuddyRemove" + e.getMessage(),"e");
            }
	}

        @Override
	public void respond(OutputStream output) {

            // Create sendBuddyRemove response
            ResponseBuddyRemove response = new ResponseBuddyRemove();

            // Set response status.
            if(toRemove == null) {
                response.setStatus((short) 0);
            }
            else {
                // gameServer.updateUserQueue(response, receiver);
                response.setStatus((short) 1);
            }     
            response.set(gameServer);
            response.setOut(output);
            response.run();

	}
}